﻿using Lamex.Base;
using Lamex.Library;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Lamex.Interface;

namespace Lamex.mDashboard.UI
{
    public partial class FrmPPC_HpoDataRequry : frmBaseChild
    {
        public FrmPPC_HpoDataRequry()
        {
            InitializeComponent();
            DateEditFormat();
            BoundData();
        }

        private void FrmPPC_HpoDataRequry_Load(object sender, EventArgs e)
        {
            try { this.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
        }
        public void DateEditFormat()
        {
            this.dateEdit1.Properties.DisplayFormat.FormatString = "yyMMdd";
            this.dateEdit1.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dateEdit1.Properties.EditFormat.FormatString = "yyMMdd";
            this.dateEdit1.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dateEdit1.Properties.Mask.EditMask = "yyMMdd";

            this.dateEdit2.Properties.DisplayFormat.FormatString = "yyMMdd";
            this.dateEdit2.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dateEdit2.Properties.EditFormat.FormatString = "yyMMdd";
            this.dateEdit2.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dateEdit2.Properties.Mask.EditMask = "yyMMdd";
        }
           /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        public override void DoView(Lamex.Interface.IButtonInfo sender)
        {
            //一定要选择时间
            if (string.IsNullOrEmpty(dateEdit1.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请选择开始时间", "提示", MessageBoxButtons.OK);
                return;
            }
            if (string.IsNullOrEmpty(dateEdit2.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请选择结束时间", "提示", MessageBoxButtons.OK);
                return;
            }
            string strOptimized = string.IsNullOrEmpty(textEdit1.Text) ? "" : textEdit1.Text;
            string startDate = string.IsNullOrEmpty(dateEdit1.Text) ? "" : dateEdit1.Text;
            string endDate = string.IsNullOrEmpty(dateEdit2.Text) ? "" : dateEdit2.Text;
            string type = string.IsNullOrEmpty(lookUpEdit1.Text) ? "" : lookUpEdit1.Text;
            gridControl1.DataSource = GETData(startDate, endDate, type, strOptimized);
            this.gridView1.SelectAll();//默认选中所有
            // gridView1.OptionsBehavior.AutoPopulateColumns = false;
            gridView1.BestFitColumns();
            this.gridView1.OptionsView.ColumnAutoWidth = false;
            base.DoView(sender);
        }
        public DataTable GETData(string startDate, string endDate, string type, string strOptimized)
        {


            string sql = @"  select s.OPTIMIZENAME,
  s.PLANNAME,
  b.COMPONENT_NAME,
  b.COMPONENT_LENGTH+'*'+b.COMPONENT_WIDTH+'*'+b.COMPONENT_THICK as SIZE,
  M.Material_IDNo,
  M.Description,
  b.QUANTITY_COMPONENT,
  N'PC' AS PC,
  (cast(WORK_UTILIZATION_RATIO as varchar)+'%') as WORK_UTILIZATION_RATIO
  from [Edgeband_Program].[dbo].HPO_TABLE_SUMMARY S LEFT JOIN
  [Edgeband_Program].[dbo].HPO_TABLE_BOARD B ON S.OPTIMIZENAME=B.OPTIMIZENAME AND S.PLANNAME=B.PLANNAME and b.QUANTITY_COMPONENT>0
 LEFT JOIN [StorageManager].[dbo].[T3_Material] M ON B.COMPONENT_NAME=M.Decor AND B.COMPONENT_LENGTH=M.Mat_Length AND B.COMPONENT_WIDTH=M.Mat_Width AND B.COMPONENT_THICK=M.Mat_Thickness
  WHERE 1=1 ";
            if (radioGroup1.SelectedIndex == 1)
            {
                sql = sql + "and S.FLAG is null ";
            }
            if (type == "")
            {
                sql = sql + @"and (  S.OPTIMIZENAME
  between 'X_" + startDate + "' and 'X_" + endDate + "' or S.OPTIMIZENAME between 'O_" + startDate + "' and 'O_" + endDate + "' ) and  S.OPTIMIZENAME like '%" + strOptimized + "%'";
            }
            else if (type == "O_")
            {
                sql = sql + @"and ( S.OPTIMIZENAME between 'O_" + startDate + "' and 'O_" + endDate + "' ) and  S.OPTIMIZENAME like '%" + strOptimized + "%'";
            }
            else if (type == "X_")
            {
                sql = sql + @"and ( S.OPTIMIZENAME between 'X_" + startDate + "' and 'X_" + endDate + "' ) and  S.OPTIMIZENAME like '%" + strOptimized + "%'";
            }


            DataTable dt = new DataTable();
            dt = BaseBuildSql.GetDataTableBySql(sql);
            return dt;
        }

        public override void DoExport(IButtonInfo sender)
        {
            int[] rownumber = this.gridView1.GetSelectedRows();//获取选中行号；
            string sql = "";
            string strOptimized = "";
            string strPlaned = "";


            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                gridControl1.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("导出成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                return;
            }
            foreach (var item in rownumber)
            {

                //已导出的Update为1
                DataRow row = this.gridView1.GetDataRow(item);//根据行号获取相应行的数据；
                strOptimized = string.IsNullOrEmpty(row[0].ToString()) ? "" : " and OPTIMIZENAME='" + row[0].ToString() + "'";
                strPlaned = string.IsNullOrEmpty(row[1].ToString()) ? "" : " and PLANNAME='" + row[1].ToString() + "'";
                sql = strOptimized + strPlaned;
                string sqlUpdate = @" UPDATE [Edgeband_Program].[dbo].[HPO_TABLE_SUMMARY] SET FLAG='1' where 1=1 ";
                sqlUpdate = sqlUpdate + sql;
                int m = BaseBuildSql.ExecuteNonQuery(sqlUpdate);
            }
        }

        private void BoundData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Name");
            DataRow newRow;
            newRow = dt.NewRow();
            newRow["ID"] = "";
            newRow["Name"] = "ALL";
            dt.Rows.Add(newRow);
            newRow = dt.NewRow();
            newRow["ID"] = "O_";
            newRow["Name"] = "正常单";
            dt.Rows.Add(newRow);
            newRow = dt.NewRow();
            newRow["ID"] = "X_";
            newRow["Name"] = "补料单";
            dt.Rows.Add(newRow);

            //清空所有列
            lookUpEdit1.Properties.Columns.Clear();
            //设置列显示和列标题
            lookUpEdit1.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("ID", "补料代码"));
            lookUpEdit1.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Name", "补料类型"));

            //绑定
            lookUpEdit1.Properties.DataSource = dt;
            lookUpEdit1.Properties.ValueMember = "Name";
            lookUpEdit1.Properties.DisplayMember = "ID";
            //是否显示列标题
            lookUpEdit1.Properties.ShowHeader = true;
            //是否显示 关闭按钮
            lookUpEdit1.Properties.ShowFooter = false;

            //为空时的值
            lookUpEdit1.Properties.NullText = "";
            //清空为空时的值
            //lookUpEdit1.Properties.NullText = null;
            //下拉项的个数
            lookUpEdit1.Properties.DropDownRows = 5;
            //默认选择第一项
            lookUpEdit1.ItemIndex = 0;
            //如果LookUpEdit无值，默认显示NullText
            //lookUpEdit1.ItemIndex = -1;
            //输入类型 Standard=可以输入   DisableTextEditor=不可以输入
            lookUpEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            //输入后是否立即弹出下拉框
            lookUpEdit1.Properties.ImmediatePopup = true;


        }
    }
}
